
class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
}


class Solution {
    public boolean isSymmetric(TreeNode root) {
        return isSymmetricChild(root.left,root.right);

    }
    //判断一棵树是否对称的底层方法
    public boolean isSymmetricChild(TreeNode left,TreeNode right){
        //两棵树根节点都为空则对称，直接返回true
        if(left==null&&right==null){
            return true;
        }
        //两棵树一棵根节点为空，另一颗不为空，不对称直接返回false
        if(left==null&&right!=null||left!=null&&right==null){
            return false;
        }
        //两棵树均不为空，比较节点的值，若不同则不对称，直接返回false
        if(left.val!=right.val){
            return false;
        }
        //递归判断左树的左子树与右树的右子树是否对称，左树的右子树与右树的左子树是否对称
        return isSymmetricChild(left.left,right.right)&&isSymmetricChild(left.right,right.left);
    }
}